﻿using System;
using Microsoft.Xna.Framework.Graphics;
using ShevaEngine.Core.Interfaces;
using ShevaEngine.Core.Modules.Graphics.Cameras;
using ShevaEngine.Core.Modules.Scenes;
using Microsoft.Xna.Framework;


namespace ShevaEngine.Core.Modules.Graphics
{
    /// <summary>
    /// Graphics viewport.
    /// </summary>
    public class GraphicsViewport
    {
        /// <summary>PosX.</summary>
        public Rectangle PositionSize;
        /// <summary>Camera.</summary>
        public Camera Camera;
        /// <summary>Scene.</summary>
        public Scene Scene;
        /// <summary>Renderer.</summary>
        public IRenderer Renderer;
        /// <summary>Gui.</summary>
        public IGui Gui;

        /// <summary>Aspect ratio.</summary>
        public float AspectRatio { get { return (float)this.PositionSize.Width / (float)this.PositionSize.Height; } }

        /// <summary>Viewport resize event.</summary>
        public event OnViewportResizeCallback ViewportResize;


        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public GraphicsViewport(Rectangle positionSize)
        {
            this.PositionSize = positionSize;
        }

        /// <summary>
        /// Draw method.
        /// </summary>
        public Texture2D Draw()
        {
            return this.Renderer.Draw();
        }

        /// <summary>
        /// Method changes width and height.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void ResizeViewport(int width, int height)
        {
            this.PositionSize.Width = width;
            this.PositionSize.Height = height;

            if (this.Camera != null)
                this.Camera.CreateProjectionMatrix(this.AspectRatio);

            if (this.ViewportResize != null)
                this.ViewportResize(this, this);
        }
    }

    /// <summary>
    /// Viewport resize callback.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="viewport"></param>
    public delegate void OnViewportResizeCallback(object sender, GraphicsViewport viewport);
}
